package com.hsmpay.mobile.service.account.impl;

import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.mobile.dao.organization.OrganizationDao;
import com.hsmpay.mobile.pojo.organization.Organization;
import com.hsmpay.mobile.service.account.OrganizationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhy on 15/11/19.
 */
@Service("organizationService")
public class OrganizationServiceImpl extends BaseServiceSupport<Organization, Long> implements OrganizationService<Organization, Long> {

    private OrganizationDao<Organization,Long> organizationDao;
    @Resource(name="organizationDao")
    public void setOrganizationDao(OrganizationDao<Organization,Long> organizationDao) {
        this.organizationDao = organizationDao;
    }

    /**
     * 获取子节点
     * @return
     * @throws Exception
     */
    public List<Organization> searchSonList(Long parentId) throws Exception{
        return organizationDao.searchSonList(parentId);
    }

    /**
     * 根据Id 查询机构   包括删除的 和没有审核通过的
     * @param id
     * @return
     * @throws Exception
     */
    public Organization searchOrganizationAllById(Long id) throws Exception{
        return organizationDao.searchOrganizationAllById(id);
    }

    /**
     * 查询所有父级名称  2及父级-3级父级-...
     * @param parentOrgId
     * @return
     * @throws Exception
     */
    public String getAllParentNameByParentOrgId(Long parentOrgId)throws Exception{
        Organization org = organizationDao.searchOrganizationAllById(parentOrgId);
        if(org.getType().intValue() <= 1){
            return org.getName();
        }
        StringBuilder sb = new StringBuilder();
        String tempNames = null;
        String split = "--";
        sb.append(org.getName());
        while(org.getType().intValue() > 1){
            org = organizationDao.searchOrganizationAllById(org.getParentId());
            tempNames = sb.toString();
            sb = new StringBuilder();
            sb.append(org.getName()).append(split).append(tempNames);
        }
        return sb.toString();
    }
}
